package com.hhkj.api.invoice.controller;

import com.hhkj.api.invoice.model.req.ApiInvoiceTitleReq;
import com.hhkj.api.invoice.service.biz.ApiSaleInvoiceTitleBizService;
import com.hhkj.api.sale.common.session.model.LoginSale;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.login.api.ApiLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ApiSaleInvoiceController
 *
 * @author zheng
 * @since 2023/11/5 8:25
 */
@RequestMapping("/api/sale/invoice/title")
@RestController
public class ApiSaleInvoiceTitleController extends BaseController {

    @Autowired
    private ApiSaleInvoiceTitleBizService invoiceTitleBizService;

    /** 根据id列表删除 */
    @ApiLogin(LoginSale.class)
    @DeleteMapping("/{idList}")
    public AjaxResult removeByIdList(@PathVariable("idList") List<Long> idList) {
        return invoiceTitleBizService.removeByIdList(idList) ? AjaxResult.success() : AjaxResult.error();
    }

    /** 获取默认发票抬头 */
    @ApiLogin(LoginSale.class)
    @GetMapping("/default")
    public AjaxResult getDefaultInvoiceTitle() {
        return AjaxResult.success(invoiceTitleBizService.getDefaultInvoiceTitle());
    }

    /** 列表 */
    @ApiLogin(LoginSale.class)
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ApiInvoiceTitleReq req) {
        return getDataTable(invoiceTitleBizService.list(req));
    }

    /** 新增 */
    @ApiLogin(LoginSale.class)
    @PostMapping()
    public AjaxResult save(@Validated @RequestBody ApiInvoiceTitleReq req) {
        return invoiceTitleBizService.save(req) ? AjaxResult.success() : AjaxResult.error();
    }

    /** 新增 */
    @ApiLogin(LoginSale.class)
    @PutMapping()
    public AjaxResult update(@Validated @RequestBody ApiInvoiceTitleReq req) {
        return invoiceTitleBizService.update(req) ? AjaxResult.success() : AjaxResult.error();
    }

    /** 新增 */
    @ApiLogin(LoginSale.class)
    @GetMapping("/{id}")
    public AjaxResult detail(@PathVariable("id") Long titleId) {
        return AjaxResult.success(invoiceTitleBizService.get(titleId));
    }
}
